<template>
  <div>
    <el-input style="width: 200px; margin: 0 10px" placeholder="请输入市名" suffix-icon="el-icon-search"
              v-model="name"/>
    <el-input style="width: 200px; margin: 0 10px" placeholder="请输入人口" suffix-icon="el-icon-search" @input="validatePopulation"
              v-model.number="population"/>
    <el-button type="primary" @click="search">搜索</el-button>
    <div>
      <el-button type="primary" style="margin: 10px" @click="dialogFormVisible = true">新增<i
          class="el-icon-circle-plus"></i></el-button>
      <el-button type="danger" style="margin: 10px" @click="handleDelBatch">批量删除<i
          class="el-icon-remove-outline"></i></el-button>
      <el-button type="primary" style="margin: 10px">导入<i class="el-icon-upload2"></i></el-button>
      <el-button type="primary" style="margin: 10px" @click="exp">导出<i class="el-icon-download"></i></el-button>
    </div>

    <el-dialog title="收货地址" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="城市名" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="人口" :label-width="formLabelWidth">
          <el-input v-model="form.population" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="活动区域" :label-width="formLabelWidth">
          <el-select v-model="form.region" placeholder="请选择活动区域">
            <el-option label="区域一" value="shanghai"></el-option>
            <el-option label="区域二" value="beijing"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleSave">确 定</el-button>
      </div>
    </el-dialog>

    <el-table :data="tableData" style="margin: 10px" border stripe>
      <el-table-column prop="id" label="ID" width="140">
      </el-table-column>
      <el-table-column prop="name" label="城市名称" width="300">
      </el-table-column>
      <el-table-column prop="population" label="人口" width="200">
      </el-table-column>
      <el-table-column prop="address" label="操作">
        <template slot-scope="scope">
          <el-button type="primary">编辑<i class="el-icon-edit"></i></el-button>
          <!--              <el-button type="danger" slot="reference">删除<i class="el-icon-remove-outline"></i></el-button>-->

          <el-popconfirm
              style="margin: 0 10px"
              confirm-button-text='确定'
              cancel-button-text='取消'
              icon="el-icon-info"
              icon-color="red"
              title="确定删除吗？"
              @confirm="handleDel(scope.row.id)"
          >
            <el-button type="danger" slot="reference">删除<i class="el-icon-remove-outline"></i></el-button>
          </el-popconfirm>

        </template>
      </el-table-column>
    </el-table>
    <div class="block" style="padding: 5px">

      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>
  </div>
</template>
<script>
import service from "@/utils/request";

export default {
  created() {
    this.load();


  },
  data() {

    return {
      // tableData: Array(15).fill(item),
      tableData: [],
      pageSize: 10,
      total: 100,
      currentPage: 1,
      name: null,
      population: null,

      dialogFormVisible: false,
      form: {
        name: '',
        population: '',

      },
      formLabelWidth: '120px'
    }
  },
  methods: {

    load() {
      service.get('/city/getpage', {
        params: {
          pageSize: this.pageSize,
          pageNum: this.currentPage,
          name: this.name,
          population: this.population
        }
      }).then(response => {
            if (response.code == 20000) {
              this.tableData = response.data.records;
              this.total = response.data.total;
              this.currentPage = response.data.current;
              this.pageSize = response.data.size;
            }
          },
          // error => {
          //   console.log("--------------!!!!" + error)
          // }
          );

    },
    handleSizeChange(val) {
      this.pageSize = val
      this.load()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.load()
    },
    handleSave() {
      service.post('/city', this.form).then(response => {
        this.$message.success('success!');
        this.dialogFormVisible = false
      })
    },
    handleDel(id) {
      service.delete('/city?id=' + id).then(response => {
        this.$message.success('删除成功!');
        this.load()

      })
    },
    search() {
      if(!this.population.isInteger){
        return;
      }
      this.currentPage = 1
      this.load()
    },
    handleDelBatch() {
      console.log(this.$route)
      // console.log(this.$router)
    },
    validatePopulation(){
      if(!this.population.isInteger){
        this.$message.info('dfd')
        this.population = ''
      }

    },
    exp() {
      window.open('http://localhost:9090/city/export')
      // service.get('/export').then(response => {})
      //
      //


    }

  }
}
</script>

<style scoped>

</style>